using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using TomShane.Neoforce.Controls;

namespace Engine2D
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Engine2D : Application
    {
        private Library mLibrary;
        private Windows.MainWindow mMainWindow;
        private RenderTarget2D mSceneRenderTarget;
        private Scene mScene;

        public Library Library { get { return mLibrary; } }
        public Scene Scene { get { return mScene; } }

        public Engine2D()
            :base("Extended", true)
        {
            Content.RootDirectory = @"Content\";
            Manager.Content.RootDirectory = Content.RootDirectory;
            //Manager.SkinDirectory = @"..\..\..\..\..\..\Neoforce\Skins\";
            Manager.SkinDirectory = @"..\..\..\..\..\Skins\";
            
            SystemBorder = false;
            FullScreenBorder = false;
            ClearBackground = false;
            ExitConfirmation = false;
            Manager.TargetFrames = 60;

            mLibrary = new Library();

            this.Graphics.PreferredBackBufferWidth = 1280;
            this.Graphics.PreferredBackBufferHeight = 720;
            Graphics.DeviceReset += new EventHandler<System.EventArgs>(GraphicsDevice_DeviceReset);

            Manager.Input.KeyPress += new KeyEventHandler(Input_KeyPress);
        }

        void Input_KeyPress(object sender, KeyEventArgs e)
        {
            if (e.Key == Keys.Escape)
                mMainWindow.ToggleSettings();
        }

        void GraphicsDevice_DeviceReset(object sender, System.EventArgs e)
        {
            CreateSceneRenderTarget();
            BackgroundImage = mSceneRenderTarget;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            CreateSceneRenderTarget();
            mScene = new Scene(this);

            BackgroundImage = mSceneRenderTarget;
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            base.LoadContent();

            mLibrary.Add(Content.Load<Texture2D>("earth"));
            mLibrary.Add(Content.Load<Texture2D>("shuttle"));
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            base.UnloadContent();
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            mScene.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }

        protected override void DrawScene(GameTime gameTime)
        {
            base.DrawScene(gameTime);

            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            GraphicsDevice.SetRenderTarget(mSceneRenderTarget);
            GraphicsDevice.Clear(Color.Blue);
            mScene.Draw(gameTime);
            GraphicsDevice.SetRenderTarget(null);
            MainWindow.Refresh();
        }

        protected override Window CreateMainWindow()
        {
            mMainWindow = new Windows.MainWindow(Manager);
            return mMainWindow;
        }

        private void CreateSceneRenderTarget()
        {
            mSceneRenderTarget = new RenderTarget2D(Graphics.GraphicsDevice, mMainWindow.ClientWidth, mMainWindow.ClientHeight, false, SurfaceFormat.Color, DepthFormat.Depth24);
        }
    }
}
